#include "BDIODeviceLibSupportBase.h"
#include "device/IODevicePubDef.h"
#include "device/IODeviceIF.h"

BDIODeviceLibSupportBase::BDIODeviceLibSupportBase()
	: m_bButton(0)
{
	//InitialIODeviceManager();
	//RegisterDeviceListener();
}


BDIODeviceLibSupportBase::~BDIODeviceLibSupportBase()
{
	//DestroyIODeviceManager();
}


void BDIODeviceLibSupportBase::RegisterDeviceListener()
{
	for (int i = IO_DEVICE_INVALID + 1; i < IO_DEVICE_MAX; ++i) {
		IODeviceIF* pDevice = GetDevice(static_cast<IODeviceType>(i));
		if (pDevice == NULL) {
			continue;
		}
		pDevice->AddEventListener(this);
	}
}

void BDIODeviceLibSupportBase::InitDevices()
{
	for (int i = IO_DEVICE_INVALID + 1; i < IO_DEVICE_MAX; ++i) {
		IODeviceIF* pDevice = GetDevice(static_cast<IODeviceType>(i));
		if (pDevice == NULL) {
			continue;
		}
		if (!pDevice->Init()) {
			pDevice->DeInit();
		}
	}
}

void BDIODeviceLibSupportBase::DeInitDevices()
{
	for (int i = IO_DEVICE_INVALID + 1; i < IO_DEVICE_MAX; ++i) {
		IODeviceIF* pDevice = GetDevice(static_cast<IODeviceType>(i));
		if (pDevice == NULL) {
			continue;
		}
		pDevice->DeInit();
	}
}

void BDIODeviceLibSupportBase::SendDeviceOperation(IODeviceType type, IODeviceOperation operation, unsigned int ptr)
{
	IODeviceIF* pDevice = GetDevice(type);
	if (pDevice == NULL) {
		return;
	}
	pDevice->SendOperation(operation, ptr);
}

void BDIODeviceLibSupportBase::OnIODeviceEvent(IODeviceType type, IODeviceOperation operation, unsigned int ptr)
{
	switch (type)
	{
	case IO_DEVICE_INVALID:
		break;
	case IO_DEVICE_TOBII:
		OnTobiiDeviceEvent(operation, ptr);
		break;
	case IO_DEVICE_THINKGEAR:
		OnThinkGearDeviceEvent(operation, ptr);
		break;
	case IO_DEVICE_ARDUINO_CONTROLLER:
		OnArduinoControllerEvent(operation, ptr);
		break;
	case IO_DEVICE_MAX:
		break;
	default:
		break;
	}
}

void BDIODeviceLibSupportBase::OnTobiiDeviceEvent(IODeviceOperation operation, unsigned int ptr)
{
	TobbiGazePoint* pPoint = reinterpret_cast<TobbiGazePoint*>(ptr);
	m_iGazePointX = pPoint->x;
	m_iGazePointY = pPoint->y;
	delete pPoint;
	pPoint = NULL;
}

void BDIODeviceLibSupportBase::OnThinkGearDeviceEvent(IODeviceOperation operation, unsigned int ptr)
{
	float value = *(reinterpret_cast<float*>(ptr));
	m_iAttention = (int)value;
}

void BDIODeviceLibSupportBase::OnArduinoControllerEvent(IODeviceOperation operation, unsigned int ptr)
{
	if (operation == IO_OPERATION_ARDUINO_CONTROLLER_GET_BUTTON_VALUE) {
		ArduinoControllerData* pData = reinterpret_cast<ArduinoControllerData*>(ptr);
		for (int i = 0; i < pData->buttonCount; ++i) {
			m_bButton.reset(i);
			if (pData->buttonValue & 1) {
				m_bButton.set(i);
			}
			pData->buttonValue >>= 1;
		}
	}
	else if (operation == IO_OPERATION_ARDUINO_CONTROLLER_GET_ROCKER_VALUE) {
		ArduinoControllerData* pData = reinterpret_cast<ArduinoControllerData*>(ptr);
		m_iRockerX1 = 5 - pData->x1;
		m_iRockerY1 = 5 - pData->y1;
		m_iRockerX2 = 5 - pData->x2;
		m_iRockerY2 = 5 - pData->y2;
	}
}


void BDIODeviceLibSupportBase::GetRockerValue(int& rockerX1, int& rockerY1, int& rockerX2, int& rockerY2)
{
	rockerX1 = m_iRockerX1;
	rockerY1 = m_iRockerY1;
	rockerX2 = m_iRockerX2;
	rockerY2 = m_iRockerY2;
}

bool BDIODeviceLibSupportBase::GetButtonIsDown(int index)
{
	return m_bButton[index];
}

bool BDIODeviceLibSupportBase::GetButtonIsDown()
{
	return m_bButton.any();
}

void BDIODeviceLibSupportBase::ClearButton()
{
	m_bButton.reset();
}

void BDIODeviceLibSupportBase::GetAttentionValue(int& attention)
{
	attention = m_iAttention;
}

void BDIODeviceLibSupportBase::GetGazePoint(int& x, int& y)
{
	x = m_iGazePointX;
	y = m_iGazePointY;
}